package model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import org.jfree.data.general.DefaultPieDataset;

public class Model {
	

	private String filename = null;
	private static String ordnerFileName = "settings/OrdnerFile.db";
	private Vector <Frage> fragen = new Vector<Frage>();
	private Vector <FragenOrdner> fragenOrdner = new Vector<FragenOrdner>();
	private double richtigeAntworten = 0.0;
	private double falscheAntworten = 0.0;
	private DefaultPieDataset data = new DefaultPieDataset();


	
	public Model() {
	}
	
	@SuppressWarnings("unchecked")
	public Vector<FragenOrdner> loadFragenOrdner() {
		Vector<FragenOrdner> input = null;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		
		File ordnerFileNameFolder = new File(ordnerFileName.split("/")[0]);
		File ordnerFile = new File(ordnerFileName); 
		
		if (ordnerFileNameFolder.exists() && ordnerFileNameFolder.isDirectory()) {
			
			if (ordnerFile.exists() && ordnerFile.isFile()) {
				
					try {
					fis = new FileInputStream(ordnerFileName);
					in = new ObjectInputStream(fis);
					input =  (Vector<FragenOrdner>) in.readObject();
					in.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				} catch (ClassNotFoundException ex) {
					ex.printStackTrace();
				}
			}

		} else {			
			ordnerFileNameFolder.mkdir();
			try {
				ordnerFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			saveFragenOrdner();
			input = fragenOrdner;
		}

		
		return input;
	}
	
	public void saveFragenOrdner() {
		FileOutputStream fos = null;
		ObjectOutputStream out = null;

		try {
			fos = new FileOutputStream(ordnerFileName);
			out = new ObjectOutputStream(fos);
			out.writeObject(this.fragenOrdner);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
 
	@SuppressWarnings("unchecked")
	public Vector<Frage> loadObjects() {
		Vector<Frage> input = null;
		FileInputStream fis = null;
		ObjectInputStream in = null;

		File questionFile = new File(filename);

			if (questionFile.exists() && questionFile.isFile()) {				
				try {
					fis = new FileInputStream(filename);
					in = new ObjectInputStream(fis);
					input =  (Vector<Frage>) in.readObject();
					in.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				} catch (ClassNotFoundException ex) {
					ex.printStackTrace();
				}
			} 
			
		return input;
	}
	
	public void saveObjects() {
		FileOutputStream fos = null;
		ObjectOutputStream out = null;

		try {
			fos = new FileOutputStream(filename);
			out = new ObjectOutputStream(fos);
			out.writeObject(this.fragen);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public Vector<Frage> sortieren(Vector<Frage> fragenVector) {
		for (int i = 0; i < fragenVector.size(); i++) {
			for (int j = 0; j < fragenVector.size(); j++) {
				if (!((j + 1) >= fragenVector.size())) {
					if (fragenVector.get(j).getKoeffizient() > fragenVector
							.get(j + 1).getKoeffizient()) {
						Frage temp = fragenVector.get(j);
						fragenVector.set(j, fragenVector.get(j + 1));
						fragenVector.set(j + 1, temp);
					}
				}
			}
		}
		return fragenVector;
	}
	
	public void addQuestion(String question,
			String answer1, String answer2, String answer3, String answer4,
			String answer5, int solution, String bildPfad) {
		
		Vector<Antwort> antworten = new Vector<Antwort>();

		antworten.add(new Antwort(answer1, false));
		antworten.add(new Antwort(answer2, false));
		antworten.add(new Antwort(answer3, false));
		antworten.add(new Antwort(answer4, false));
		antworten.add(new Antwort(answer5, false));

		int tempSolution = solution - 1;

		for (int i = 0; i < antworten.size(); i++) {
			if (i == tempSolution) {
				antworten.get(i).setRichtig(true);
			}
		}

		Frage frage = new Frage(question, antworten);
		
		if (bildPfad != null) {
			frage.setBildPfad(bildPfad);

		}
		this.fragen.addElement(frage);
	}


	public Vector<Frage> getFragen() {
		return fragen;
	}

	public void setFragen(Vector<Frage> fragen) {
		this.fragen = fragen;
	}

	public DefaultPieDataset getData() {
		return data;
	}

	public void setData(DefaultPieDataset data) {
		this.data = data;
	}

	public double getRichtigeAntworten() {
		return richtigeAntworten;
	}

	public void setRichtigeAntworten(double richtigeAntworten) {
		this.richtigeAntworten = richtigeAntworten;
	}

	public double getFalscheAntworten() {
		return falscheAntworten;
	}

	public void setFalscheAntworten(double falscheAntworten) {
		this.falscheAntworten = falscheAntworten;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public Vector<FragenOrdner> getFragenOrdner() {
		return fragenOrdner;
	}

	public void setFragenOrdner(Vector<FragenOrdner> fragenOrdner) {
		this.fragenOrdner = fragenOrdner;
	}
}
